package com.zjj.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

public class CommonUtil {
	
	// 结果集状态码：0、成功 1、失败
	public static final String RESULT_CODE = "RESULT_CODE";
	
	public static final String RESULT_MESSAGE = "RESULT_MESSAGE";
	
	public static final String LOGIN_USER_SESSION_KEY = "USER";
	
	public static final String RET_STATUS = "RET_STATUS";
	
	public static final String LIST = "LIST";
	
	public static final String TOTAL_NUM = "TOTAL_NUM";
	
	
	/**
	 * 封装结果集
	 * 
	 * @param resultCode
	 * @param resultMessage
	 * @return
	 */
	public static Map<String, Object> resultMap(int resultCode, String resultMessage) {
		Map<String, Object> map = new HashMap<>();
		map.put(CommonUtil.RESULT_CODE, resultCode);
		map.put(CommonUtil.RESULT_MESSAGE, resultMessage);
		return map;
	}
	
	/**
	 * 检测结果集： true 无效的
	 * 
	 * @param result
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isInvalid(Map result) {
		if (result == null) {
			return true;
		}
		String status = (String) result.get(RET_STATUS);
		return !"S".equalsIgnoreCase(status);
	}
	
	/**
	 * 用于查询结果集列表时返回的对象
	 * 
	 * @param list
	 * @param totalNum
	 * @return
	 */
	public static Map<String, Object> queryListResultMap(List<Map<String, Object>> list, String totalNum) {
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(CommonUtil.LIST, list);
		map.put(CommonUtil.TOTAL_NUM, totalNum);
		return map;
	}
	
	/**
	 * 封装页面请求参数的分页对象
	 * @param request
	 * @return
	 */
	public static PageBean getPageObject(HttpServletRequest request) {
		String currentPage = request.getParameter("page");
		String size = request.getParameter("size");
		PageBean page = new PageBean();
		if (StringUtils.isBlank(currentPage) || StringUtils.isBlank(size)) {
			return page;
		}
		page.setCurrentPage(Integer.parseInt(currentPage));
		page.setPageSize(Integer.parseInt(size));
		return page;
	}
	
}
